import {createRouter, createWebHistory} from 'vue-router'
import {guard} from '@/router/guard'

import ROUTES from './modules/index'
import {useAuth} from '@/stores/auth'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: ROUTES
})

function removeRouter(menu) {
    menu.forEach((item) => {
        if (item.children && item.children.length > 0) {
            removeRouter(item.children)
        } else {
            router.removeRoute(item.name)
        }
    })
}

router.resetRouter = () => {
const authStore = useAuth()
    removeRouter(authStore.menu)
}
guard(router)

export default router
